/*============================================================================
  File:      DataValues.h
 
  Summary:   Declaration of functions used to simplify representation of data
			 values.

  Date:		 June 30, 2004
------------------------------------------------------------------------------
  This file is part of the Microsoft SQL Server Code Samples.
 
  Copyright (C) 2003 Microsoft Corporation.  All rights reserved.
 
This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.
 
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
============================================================================*/

#pragma once

#include "..\..\dmalgo.h" 
#include <float.h>
#include <assert.h>

typedef double DBL;

// Analysis Services stores the values for variables in the 
// DM_STATE_VALUE structure, which contains both a double (r8) and
// a DM_STATE_INDEX (si). The DM_STATE_INDEX structure contains
// two ULONGs: iFlag and iState.

// Continuous variables are stored using r8 member of the DM_STATE_VALUE,
// with the exception that if the value is missing, this is indicated by
// the iFlag of the si being equal to a magic constant 0xFFF80000L.
//
// Categorical variables are stored using the iState member of the si.
//
// Categorical variables in Analysis Services are NEVER missing: state zero
// always means missing, which can be problematic from a modeling point 
// of view. 
//
// This file contains conversions between the Analysis Services' representation
// for variable values and a simplified representation that may be easier to
// use for modeling. In the simplified version, all values are represented as doubles,
// regardless of whether the variable is continuous or categorical.
//
// In the simplified version, we have a single representation for missing:

const double dblMissing = DBL_MAX;

// Continuous variables are converted in the obvious way:

DBL	DblContinuous(DM_STATE_VALUE& dmstatevalue);
DM_STATE_VALUE	DmstatevalueContinuous(DBL dbl);

//
// There are two flavors of categorical-variable transformation:
//
// (1) Missing-at-random categorical variables
//
//		For this transformation, all state 0 values from AS are converted to
//		dblMissing, and all other state values are decremented by one. This
//		leads to a categorical variable with states that are zero based.

DBL	DblCategoricalMAR(DM_STATE_VALUE& dmstateval);
DM_STATE_VALUE	DmstatevalueCategoricalMAR(DBL dbl);

// (2) Not-missing-at-random categorical variable
//
//		For this transformation, the states of categorical variables are
//		left as-is.

DBL	DblCategorical(DM_STATE_VALUE& dmstatevalue);
DM_STATE_VALUE	DmstatevalueCategorical(DBL dbl);

